<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class SoftWareController extends Controller
{

    public function WeaStat(){
        return [
            0=>'晴',1=>'阴',2=>'雨',3=>'雪',4=>'多云',5=>'冰雹',6=>'雾霾',7=>'沙尘'
        ];
    }

    /**
     * 安全帽
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function aqm(Request $request) {
        //防软件页面
        $data = [];
        //安全带所有标准
        $toolID = DB::table('a_tool')->select('id')->where('name','安全帽')->first();
        $sql = "SELECT
            a_std_tool_rel.id,
            a_standard.num,
            a_std_tool_rel.cycle,
            a_std_tool_rel.remark,
            a_std_tool_rel.sort,
            a_std_tool_rel.ctime
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id = :id";
        $data_rel = DB::select($sql,['id'=>$toolID->id]);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='F' and tool_id = :id";
        $data_lizhi = DB::select($lisql,['id'=>$toolID->id]);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id = :id";
        $data_time = DB::select($tSql,['id'=>$toolID->id]);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61 and i.name not LIKE '%交流%' and s.tool_id=:id";
        $data_item = DB::select($itemsql,['id'=>$toolID->id]);
        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.aqm', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }
    /**
     * 梯具
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function tiju(Request $request) {
        //防软件页面
        $data = [];
        //安全带所有标准
        $toolID = DB::table('a_tool')->whereIn('name',['软梯','硬梯','绝缘硬梯','绝缘软梯','复合材料梯（绝缘梯）','竹木梯','铝合金梯'])->pluck('id')->toArray();
        $toolID = implode($toolID, ',');

        $sql = "SELECT
            a_std_tool_rel.id,
            a_standard.num,
            a_std_tool_rel.cycle,
            a_std_tool_rel.remark,
            a_std_tool_rel.sort,
            a_std_tool_rel.ctime
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id in ($toolID)";
        $data_rel = DB::select($sql);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='F' and tool_id in ($toolID)";
        $data_lizhi = DB::select($lisql);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id in ($toolID)";
        $data_time = DB::select($tSql);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61 and s.tool_id in ($toolID)";
        $data_item = DB::select($itemsql);
        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.tiju', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }
    /**
     * 教扣登高板
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function jiaokou(Request $request) {
        //防软件页面
        $data = [];
        //安全带所有标准
        $toolID = DB::table('a_tool')->select('id')->where('name','登杆脚扣')->first();
        $sql = "SELECT
            a_std_tool_rel.id,
            a_standard.num,
            a_std_tool_rel.cycle,
            a_std_tool_rel.remark,
            a_std_tool_rel.sort,
            a_std_tool_rel.ctime
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id = :id";
        $data_rel = DB::select($sql,['id'=>$toolID->id]);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='F' and tool_id = :id";
        $data_lizhi = DB::select($lisql,['id'=>$toolID->id]);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id = :id";
        $data_time = DB::select($tSql,['id'=>$toolID->id]);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61 and s.tool_id=:id";
        $data_item = DB::select($itemsql,['id'=>$toolID->id]);
        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.jiaokou', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }
    /**
     * 抗弯动静负荷试验机
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function kangwan(Request $request) {
        //防软件页面
        $data = [];
        //安全带所有标准
        $toolID = DB::table('a_tool')->whereIn('name',['绝缘操作杆','绝缘托瓶架'])->pluck('id')->toArray();
        $toolID = implode($toolID, ',');

        $sql = "SELECT DISTINCT 
            a_std_tool_rel.id,
            a_standard.num
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id in ($toolID)";
        $data_rel = DB::select($sql);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.unit like BINARY '%N%' and tool_id in ($toolID)";
        $data_lizhi = DB::select($lisql);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id in ($toolID)";
        $data_time = DB::select($tSql);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61  and i.name not LIKE '%耐压%' and s.tool_id in ($toolID)";
        $data_item = DB::select($itemsql);
        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.kangwan', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }

    /**
     * 卧拉
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function wola(Request $request) {
        //防软件页面
        $data = [];
        //所有标准
        $toolID = DB::table('a_tool')->whereIn('name',['安全绳','绝缘软梯','复合材料梯（绝缘梯）','登杆脚扣'])->pluck('id')->toArray();
        $toolID = implode($toolID,',');
        $sql = "SELECT  
            id,
            num
        FROM
             a_standard 
        ORDER BY id asc";
        $data_rel = DB::select($sql);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='F' and tool_id in ($toolID)";
        $data_lizhi = DB::select($lisql);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id in ($toolID)";
        $data_time = DB::select($tSql);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61 and s.tool_id in ($toolID)";
        $data_item = DB::select($itemsql);
        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.wola', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }
    /**
     * 安全带设备试验页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function anQuanDai(Request $request) {
        //防软件页面
        $data = [];
        //安全带所有标准
        $toolID = DB::table('a_tool')->select('id')->where('name','安全带')->first();
        $sql = "SELECT
            a_std_tool_rel.id,
            a_standard.num,
            a_std_tool_rel.cycle,
            a_std_tool_rel.remark,
            a_std_tool_rel.sort,
            a_std_tool_rel.ctime
        FROM
            a_std_tool_rel
        LEFT JOIN a_standard ON a_std_tool_rel.std_id = a_standard.id
        WHERE
            a_std_tool_rel.tool_id = :id";
        $data_rel = DB::select($sql,['id'=>$toolID->id]);
        //所有力值
        $lisql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='F' and tool_id = :id";
        $data_lizhi = DB::select($lisql,['id'=>$toolID->id]);
        //所有时间
        $tSql = "select DISTINCT
            a.key,
            a.unit,
            b.val 
        from 
            a_condition_key as a 
        join a_condition as b on a.id = b.condition_key_id 
        where a.key='t' and tool_id = :id";
        $data_time = DB::select($tSql,['id'=>$toolID->id]);
        //所有试验项目
        $itemsql = "select DISTINCT 
            s.testitem_id,
            i.name 
        from a_std_tool_item_rel as s 
        join a_testitem as i on s.testitem_id = i.id 
        where s.testitem_id !=61 and s.tool_id=:id";
        $data_item = DB::select($itemsql,['id'=>$toolID->id]);

        //温湿度大气压
        $temp_humi = DB::table('a_env_info')->where('temp','!=','')->orderByDesc('id')->first();
        $pressure = DB::table('a_env_info')->where('pressure','!=','')->orderByDesc('id')->first();
        $weaStat = $this->WeaStat();

        return view('admin.software.anquandai', compact('data','data_rel','data_lizhi','data_time','data_item','temp_humi','pressure','weaStat'));
    }

    /**
     * 根据试吕编号获取样品试验标准，试验项目，再通过选择试验项目展示试验条件与关联值，太jb麻烦了，此方案下马
     * @param Request $request
     * @return false|string
     */
    public function getGqjInfo(Request $request){
        $bar_code = $request->bar_code;
        if(!$bar_code){
            $res = array('code'=>0,'msg'=>'缺少条形码','data'=>'');
            return json_encode($res);
        }
        $gqjInfo = DB::table('a_sample_info as s')->leftJoin('a_contract_gaiyao as c','s.contract_gaiyao_id','=','c.id')->select('s.id as sample_id','s.bar_code','s.contract_id','s.contract_gaiyao_id','c.std_num','c.std_id','c.tool_name','c.tool_id','c.item_ids')->where('s.bar_code',$bar_code)->orWhere('s.third_id', $bar_code)->get()->map(function ($value) {
            return (array)$value;
        })->toArray();
        $gqjInfo = $gqjInfo[0];
        //获取试验项目
        $itemIds = explode(',',$gqjInfo['item_ids']);
        $item_ids = DB::table('a_testitem')->select('id','name')->whereIn('id',$itemIds)->where('id','!=',61)->get()->map(function ($value) {
            return (array)$value;
        })->toArray();
        $gqjInfo['item_names'] = $item_ids;
        $res = array(
            'code'=>1,
            'msg'=>'',
            'data'=>$gqjInfo
        );
        $data = json_encode($res);
        return $data;

    }

    /**
     * @param Request $request
     * @return string
     * 保存数据
     */
    public function saveDataAjax(Request $request) {
        $arrRes = array(
            'start{0}end'=>'上传成功',
            'start{1}end'=>'用户无效',
            'start{2}end'=>'未受理该样品',
            'start{3}end'=>'未受理检测项目',
            'start{4}end'=>'任务未分配',
            'start{5}end'=>'该条码已提交，不允许上传',
        );
        $dataUpload = new DataUploadController();
        if(isset($request->isAQM)){
            //走安全帽逻辑
            $result = $dataUpload->AQM_upload($request);
        }else {
            //走拉力机逻辑

            $result = $dataUpload->LLJ_upload($request);
        }
        return $arrRes[$result];
    }

}
